using System;
using System.IO;
using System.Text;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace DNNtc.MSBuild.Tasks
{
    public class CreateFile : Task
    {
        #region Properties

        [Required]
        public string Target { get; set; }

        #endregion

        #region Events

        public override bool Execute()
        {
            try
            {
                //Create the DNN File
                BuildDnnFile();
                return true;
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
                return false;
            }
        }

        #endregion

        #region Private Methods

        private void BuildDnnFile()
        {
            //String Builder object to hols the file
            StringBuilder sb = new StringBuilder();

            //Actual dnn File
            sb.AppendLine("<dotnetnuke type=\"Package\" version=\"5.0\">");
            sb.AppendLine("@tab@<packages>");
            sb.AppendLine("@tab@@tab@<package name=\"@ModuleName@\" type=\"Module\" version=\"@version@\">");
            sb.AppendLine("@tab@@tab@@tab@<friendlyName>@FriendlyName@</friendlyName>");
            sb.AppendLine("@tab@@tab@@tab@<description>@Description@</description>");
            sb.AppendLine("@tab@@tab@@tab@<iconFile>@iconFile@</iconFile>");
            sb.AppendLine("@tab@@tab@@tab@<owner>");
            sb.AppendLine("@tab@@tab@@tab@@tab@<name>@CompanyName@</name>");
            sb.AppendLine("@tab@@tab@@tab@@tab@<organization>@CompanyName@</organization>");
            sb.AppendLine("@tab@@tab@@tab@@tab@<url>@CompanyUrl@</url>");
            sb.AppendLine("@tab@@tab@@tab@@tab@<email>@CompanyEmail@</email>");
            sb.AppendLine("@tab@@tab@@tab@</owner>");
            sb.AppendLine("@tab@@tab@@tab@<license src=\"License.txt\"></license>");
            sb.AppendLine("@tab@@tab@@tab@<releaseNotes @Releasetxt@></releaseNotes>");
            sb.AppendLine("@tab@@tab@@tab@@tab@<azureCompatible>@azureCompatible@</azureCompatible>");
            sb.AppendLine("@tab@@tab@@tab@<dependencies>");
            sb.AppendLine("@Dependencies@@tab@@tab@@tab@</dependencies>");
            sb.AppendLine("@tab@@tab@@tab@<components>");

            //Script Component
            sb.AppendLine("@tab@@tab@@tab@@tab@<component type=\"Script\">");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@<scripts>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@<basePath>DesktopModules\\@FolderName@</basePath>");
            sb.AppendLine("@ScriptComponent@@tab@@tab@@tab@@tab@@tab@</scripts>");
            sb.AppendLine("@tab@@tab@@tab@@tab@</component>");
            
            //Module Component
            sb.AppendLine("@tab@@tab@@tab@@tab@<component type=\"Module\">");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@<desktopModule>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@<moduleName>@ModuleName@</moduleName>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@<foldername>@FolderName@</foldername>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@BusinessControllerClass@");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@SupportedFeatures@");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@<moduleDefinitions>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@tab@<moduleDefinition>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@tab@@tab@<friendlyName>@FriendlyName@</friendlyName>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@tab@@tab@<defaultCacheTime>@defaultCacheTime@</defaultCacheTime>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@tab@@tab@<moduleControls>");
            sb.AppendLine("@moduleControls@@tab@@tab@@tab@@tab@@tab@@tab@@tab@@tab@</moduleControls>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@tab@@tab@<permissions>");
            sb.AppendLine("@Permissions@@tab@@tab@@tab@@tab@@tab@@tab@@tab@@tab@</permissions>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@tab@</moduleDefinition>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@</moduleDefinitions>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@</desktopModule>");
            sb.AppendLine("@EventMessage@@tab@@tab@@tab@@tab@</component>");
            
            //Assembly Component
            sb.AppendLine("@tab@@tab@@tab@@tab@<component type=\"Assembly\">");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@<assemblies>");
            sb.AppendLine("@assemblyComponent@@tab@@tab@@tab@@tab@@tab@</assemblies>");
            sb.AppendLine("@tab@@tab@@tab@@tab@</component>");
            
            //ResourceFile Component
            sb.AppendLine("@tab@@tab@@tab@@tab@<component type=\"ResourceFile\">");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@<resourceFiles>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@<basePath>DesktopModules\\@FolderName@</basePath>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@<resourceFile>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@tab@<name>resources.zip</name>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@@tab@<sourceFileName>resources.zip</sourceFileName>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@@tab@</resourceFile>");
            sb.AppendLine("@tab@@tab@@tab@@tab@@tab@</resourceFiles>");
            sb.AppendLine("@tab@@tab@@tab@@tab@</component>");

            //CleanupComponent
            sb.AppendLine("@CleanupFiles@");
            
            //ConfigComponent
            sb.AppendLine("@ConfigItems@");
            
            sb.AppendLine("@tab@@tab@@tab@</components>");
            sb.AppendLine("@tab@@tab@</package>");
            sb.AppendLine("@tab@</packages>");
            sb.AppendLine("</dotnetnuke>");

            //Output the file
            StreamWriter sw = new StreamWriter(Target, false);
            sw.Write(sb.ToString());
            sw.Close();
        }

        #endregion
    }
}